<template>
  <c-form-dialog
    ref="form"
    :form-data="formData"
    :form-items="formItems"
    :table-id="tableId"
    :table-name="tableName"
    :visible.sync="innerVisible"
    :before-save="handleBeforeSave"
    @dismiss="handleDismiss"
    @success="handleSuccess"
  ></c-form-dialog>
</template>
<script>
import { mapGetters } from 'vuex'
import { add } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    visible: Boolean,
    compsId: String,
    queryId: String,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const isNumberValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!Number.isFinite(+value)) {
        callback(new Error('请输入正确的数值'))
      } else {
        callback()
      }
    }
    return {
      innerVisible: false,
      productName: '',
      formItems: [
        {
          tag: 'date',
          itemAttrs: {
            label: '追偿时间',
            rules: [{ required: true, message: '请选择追偿时间', trigger: 'change' }],
          },
          appendSlot: 'appendSlot',
          attrs: { key: 'recovery_date', value: '', placeholder: '请选择追偿时间' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '追偿本金',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'amount', value: '', placeholder: '请输入追偿本金' },
          tagSlots: [{ name: 'append', slot: '元' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '追偿利息',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'interest', value: '', placeholder: '请输入追偿利息' },
          tagSlots: [{ name: 'append', slot: '元' }],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '追偿其他费用',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'recovery_fee', value: '', placeholder: '请输入追偿其他费用' },
          tagSlots: [{ name: 'append', slot: '元' }],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '资金占用年利率',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'funds_use_rate', value: '', placeholder: '请输入资金占用年利率' },
          tagSlots: [{ name: 'append', slot: '%' }],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '应收资金占用费',
            rules: [{ validator: isNumberValidator, trigger: 'blur' }],
          },
          attrs: { key: 'funds_use_fee', value: '', placeholder: '请输入应收资金占用费' },
          tagSlots: [{ name: 'append', slot: '元' }],
        },

        {
          tag: 'input',
          itemAttrs: {
            label: '追偿合计金额',
          },
          attrs: {
            key: 'recovery_amount',
            value: '',
            placeholder: '请输入追偿金额',
            disabled: true,
          },
          tagSlots: [{ name: 'append', slot: '元' }],
          getAttrs: model => {
            return {
              value: add(model.amount, model.interest, model.recovery_fee, model.funds_use_fee),
            }
          },
        },

        {
          tag: 'textarea',
          itemAttrs: { label: '追偿说明' },
          attrs: { key: 'des', value: '', placeholder: '请输入追偿说明', autosize: { minRows: 5 } },
        },

        {
          tag: 'upload',
          itemAttrs: {
            label: '追偿附件',
            class: 'form-control-ml',
            // rules: [{ required: true, message: '请上传追偿附件', trigger: 'change' }],
          },
          attrs: {
            key: 'attachment',
            fileName: 0,
            value: '',
            placeholder: '追偿附件',
            accept: '',
            listType: '',
            dir: `/credit/unload/${this.compsId}`,
          },
        },
      ],
      tableName: 'comps_recovery',
    }
  },
  computed: {
    ...mapGetters(['user']),
  },
  watch: {
    formData: {
      handler: function () {
        this.initFormData()
      },
      deep: true,
      immediate: true,
    },
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      model.comps_id = this.compsId
      model.customer_related_id = this.queryId
      model.submit_person_name = this.user.realName
      return model
    },
    handleDismiss() {
      this.innerVisible = false
      this.$emit('update:visible', false)
    },
    handleSuccess() {
      this.$emit('success', false)
      this.handleDismiss()
    },
    initFormData() {
      this.$refs.form?.setAttrs('attachment', { dir: `/credit/recovery/${this.compsId}` })
    },
  },
}
</script>
<style lang="scss" scoped></style>
